//
//  AppDelegate.m
//  emark
//
//  Created by neebel on 2017/5/27.
//  Copyright © 2017年 neebel. All rights reserved.
//

#import "AppDelegate.h"
#import "XuKaiAlertManager.h"
#import "XuKaiBaseNavigationController.h"
#import "XuKaiSplashViewController.h"
#import "PushHelper.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self registerLocalNotification];//注册本地通知
    [[XuKaiAlertManager sharedManager] didFinishLaunchingWithOptions:launchOptions];
    
    XuKaiSplashViewController *splashVC = [[XuKaiSplashViewController alloc] init];
    XuKaiBaseNavigationController *nav = [[XuKaiBaseNavigationController alloc] initWithRootViewController:splashVC];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];

    [PushHelper startSDKWithDelegate:self];
    [PushHelper registerRemoteNotification];
    
    
    return YES;
}
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [PushHelper resume];
    completionHandler(UIBackgroundFetchResultNewData);
}
    
    - (void)applicationDidEnterBackground:(UIApplication *)application {
        [PushHelper enterBackground];
    }
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
    
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    NSString *tdeviceToken = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    [PushHelper registerDeviceToken:tdeviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
    //如果APNS注册失败，通知个推服务器
    [PushHelper registerDeviceToken:@""];
}
    
- (void)applicationWillEnterForeground:(UIApplication *)application {
    
}
    
    
- (void)applicationDidBecomeActive:(UIApplication *)application {
    

    [PushHelper startSDKWithDelegate:self];
    
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [[XuKaiAlertManager sharedManager] didReceiveLocalNotification:notification];
}


- (void)registerLocalNotification
{
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
    }
}

@end
